/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package receptorudp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 *
 * @author Diego
 */
public class Receptor
{

    int serverPort = 7000;
    int tamanhoBuffer = 2048;
    byte[] buffer;
    DatagramSocket socket;

    public Receptor() throws SocketException
    {
        buffer = new byte[tamanhoBuffer];
        socket = new DatagramSocket(serverPort);
    }

    public DatagramPacket receber() throws IOException
    {
        DatagramPacket request = new DatagramPacket(buffer, buffer.length);
        socket.receive(request);

        this.enviaACK(request);

        return request;
    }

    public void enviaACK(DatagramPacket request) throws IOException
    {
        byte[] data = request.getData();
        byte retornar[] = new byte[2];

        retornar[0] = data[0];
        retornar[1] = (byte) 'A';
        
       //System.out.println("retornando 0:<"+retornar[0]+"> 1:<"+retornar[1]+">");
        DatagramPacket ACK;

        ACK = new DatagramPacket(retornar, retornar.length, request.getSocketAddress());

        socket.send(ACK);
    }
}
